package com.monas.client;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import com.monas.client.database.MonasResourceDatabase;
import com.monas.client.view.MainView;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	public ApplicationWorkbenchWindowAdvisor(
			IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	public ActionBarAdvisor createActionBarAdvisor(
			IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(400, 300));
		configurer.setShowCoolBar(false);
		configurer.setShowStatusLine(false);
		configurer.setTitle("Monas Client " + MainView.VERSION);
	}

	@Override
	public void postWindowOpen() {
		Shell shell = getWindowConfigurer().getWindow().getShell();
		shell.setMaximized(true);
		shell.setImage(Activator.getImageDescriptor("icons/alt_window_32.gif")
				.createImage());
	}
	
	@Override
	public void postWindowClose() {
		// Tutup koneksi database ketika aplikasi di-close
		MonasResourceDatabase database = null;
		try {
			database = MonasResourceDatabase.getInstance();
		} catch (Exception e) {
		} finally {
			if (database != null) {
				database.close();
			}
		}
		super.postWindowClose();
	}
}
